package service

import (
	"encoding/json"
	"errors"
	"haiqiu-go-admin/dao"
	"haiqiu-go-admin/entity"
	"haiqiu-go-admin/entity/dto"
	"haiqiu-go-admin/entity/vo"
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/utils"
	"log"
	"net/http"
	"strconv"
	"time"
)

// NoticeById 根据ID查询数据
func NoticeById(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson := utils.GetParamsFormatJson(w, r)
	noticeIdStr := formatJson["id"][0]
	noticeId, err := strconv.ParseInt(noticeIdStr, 10, 64)
	if err != nil {
		panic(err)
		return nil, err
	}
	notice, err := dao.NoticeById(noticeId)
	if err != nil {
		return nil, err
	}
	return &notice, nil
}

// NoticePage 分页查询
func NoticePage(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, errors.New(result.ParamsErr)
	}
	var page dto.NoticeQueryDto
	err = json.Unmarshal(formatJson, &page)
	if err != nil {
		log.Println(err)
	}
	count := dao.NoticePageCount(page)
	if count == 0 {
		return vo.PageData{
			Total: 0,
		}, nil
	}
	var pageData vo.PageData
	list, err := dao.NoticePageList(page)
	pageData.Data = list
	pageData.Total = count
	pageData.PageIndex = page.PageIndex
	pageData.PageSize = page.PageSize
	if err != nil {
		return nil, err
	}
	return pageData, nil
}

// NoticeAdd 新增数据
func NoticeAdd(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, err
	}
	var sysNotice = entity.SysNotice{}
	err = json.Unmarshal(formatJson, &sysNotice)
	if err != nil {
		return nil, err
	}
	var nowTime = time.Now()
	_, _, username, err := utils.Token{}.GetUserTokenAndUserIdAndUsername(w, r)
	if err != nil {
		return nil, err
	}
	sysNotice.Username = *username
	sysNotice.CreateTime = &nowTime
	sysNotice.UpdateTime = &nowTime
	add, err := dao.NoticeAdd(&sysNotice)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// NoticeUpdate 根据ID更新数据
func NoticeUpdate(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, err
	}
	var sysNotice = entity.SysNotice{}
	err = json.Unmarshal(formatJson, &sysNotice)
	if err != nil {
		return nil, err
	}
	var nowTime = time.Now()
	_, _, username, err := utils.Token{}.GetUserTokenAndUserIdAndUsername(w, r)
	if err != nil {
		return nil, err
	}
	sysNotice.Username = *username
	sysNotice.UpdateTime = &nowTime
	add, err := dao.NoticeUpdate(&sysNotice)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// NoticeDeleteBatch 批量删除数据
func NoticeDeleteBatch(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		return nil, err
	}
	var ids []int64
	err = json.Unmarshal(formatJson, &ids)
	if err != nil {
		return nil, err
	}

	add, err := dao.NoticeDeleteBatch(ids)
	if err != nil {
		return nil, err
	}
	return add, nil
}

// NoticeList 查询所有数据
func NoticeList(w http.ResponseWriter, r *http.Request) (*[]entity.SysNotice, error) {
	noticeList, err := dao.NoticeList()
	if err != nil {
		log.Println(err.Error())
		return nil, err
	}
	return noticeList, nil
}

func ActiveNotice(w http.ResponseWriter, r *http.Request) (any, error) {
	formatJson, err := utils.GetBodyFormatJson(w, r)
	if err != nil {
		log.Println(err.Error())
		panic(err)
		return nil, err
	}
	var sysNotice []entity.SysNotice
	err = json.Unmarshal(formatJson, &sysNotice)
	if err != nil {
		log.Println(err.Error())
		panic(err)
		return nil, err
	}
	rowsAffected, err := dao.ActiveNoticeById(sysNotice)
	return rowsAffected, err
}

func StatisticsNotice() (any, error) {
	//SELECT CASE `type` WHEN 0 THEN '公告' WHEN 1 THEN '通知' ELSE '日志' END AS typeName, `type`, count( id ) AS `count` FROM `sys_notice` GROUP BY type
	return dao.StatisticsNotice()
}
